<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="中文简称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入中文简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大洲" prop="continent">
        <el-select v-model="queryParams.continent" placeholder="请选择大洲" clearable>
          <el-option label="亚洲" value="亚洲" />
          <el-option label="欧洲" value="欧洲" />
          <el-option label="北美洲" value="北美洲" />
          <el-option label="南美洲" value="南美洲" />
          <el-option label="非洲" value="非洲" />
          <el-option label="大洋洲" value="大洋洲" />
          <el-option label="南极洲" value="南极洲" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:country:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:country:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:country:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['basic:country:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="countryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="国旗/区旗" align="center" prop="flag" width="130">
        <template #default="scope">
          <el-image
            :src="scope.row.flag"
            style="width: 100px; height: 86px"
            fit="contain"
            v-if="scope.row.flag"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><PictureFilled /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="国家ID" align="center" prop="countryId" width="80" />
      <el-table-column label="中文简称" align="center" prop="name" min-width="100" />
      <el-table-column label="大洲" align="center" prop="continent" width="80" sortable />
      <el-table-column label="国家代码" align="center" prop="code" width="80" />
      <el-table-column label="中文全称" align="center" prop="fullName" min-width="100" />
      <el-table-column label="英文简称" align="center" prop="nameEn" min-width="100" />
      <el-table-column label="英文全称" align="center" prop="fullNameEn" min-width="110" />
      <el-table-column label="成立时间" align="center" prop="beginDate" min-width="100" />
      <el-table-column label="消失时间" align="center" prop="endDate" min-width="100" />
      <el-table-column label="简介" align="left" prop="brief" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="130">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['basic:country:edit']">编辑</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['basic:country:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改国家对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="countryRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="中文简称" prop="name">
              <el-input v-model="form.name" placeholder="请输入国家/地区简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入国家/地区编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio label="0">国家</el-radio>
                <el-radio label="1">地区</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文全称" prop="fullName">
              <el-input v-model="form.fullName" placeholder="请输入国家/地区全称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属大洲" prop="continent">
              <el-select v-model="form.continent" style="width: 100%" placeholder="请选择所属大洲">
                <el-option label="亚洲" value="亚洲" />
                <el-option label="欧洲" value="欧洲" />
                <el-option label="南美洲" value="南美洲" />
                <el-option label="北美洲" value="北美洲" />
                <el-option label="大洋洲" value="大洋洲" />
                <el-option label="非洲" value="非洲" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文简称" prop="nameEn">
              <el-input v-model="form.nameEn" placeholder="请输入国家/地区英文简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文全称" prop="fullNameEn">
              <el-input v-model="form.fullNameEn" placeholder="请输入国家/地区英文全称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原名简称" prop="originalName">
              <el-input v-model="form.originalName" placeholder="请输入国家/地区原名简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原名全称" prop="originalFullName">
              <el-input v-model="form.originalFullName" placeholder="请输入国家/地区原名全称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成立日期" prop="beginDate">
              <el-date-picker
                v-model="form.beginDate"
                style="width: 100%"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择成立日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消失日期" prop="endDate">
              <el-date-picker
                v-model="form.endDate"
                style="width: 100%"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择消失日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="国旗/区旗" prop="flag">
              <el-input v-model="form.flag" placeholder="请输入国旗图片URL" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介" prop="brief">
              <el-input
                v-model="form.brief"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 8 }"
                maxlength="600"
                show-word-limit
                placeholder="国家/地区简介"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Country">
import { listCountry, getCountry, delCountry, addCountry, updateCountry } from "@/api/basic/country";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { PictureFilled } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const countryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    continent: null
  },
  rules: {
    name: [
      { required: true, message: "中文简称不能为空", trigger: "blur" }
    ],
    code: [
      { required: true, message: "国家代码不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询国家列表 */
function getList() {
  loading.value = true;
  listCountry(queryParams.value).then(response => {
    countryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    countryId: null,
    name: null,
    code: null,
    type: null,
    fullName: null,
    continent: null,
    nameEn: null,
    fullNameEn: null,
    originalName: null,
    originalFullName: null,
    beginDate: null,
    endDate: null,
    flag: null,
    brief: null
  };
  proxy.resetForm("countryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.countryId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加国家";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _countryId = row.countryId || ids.value;
  getCountry(_countryId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改国家";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["countryRef"].validate(valid => {
    if (valid) {
      if (form.value.countryId != null) {
        updateCountry(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCountry(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const countryIds = row.countryId || ids.value;
  proxy.$modal.confirm('是否确认删除国家编号为"' + countryIds + '"的数据项？').then(function() {
    return delCountry(countryIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('basic/country/export', {
    ...queryParams.value
  }, `country_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
}
</style>